#! /bin/bash


# OSX Auth
OSX_KEYCHAIN_AUTH_FILE=$CONFIG_DIR/auth/osx-keychain.auth
     OSX_KEYCHAIN_FILE=~jenkins/Library/Keychains/login.keychain

### iOS
iOS_IPA_SAVE_URL=iOS/ipa/`date +%Y/%m/%d`
iOS_IPA_SAVE_DIR=$JKS_DL_DIR/$iOS_IPA_SAVE_URL

                 iOS_DEV_CSI=""
iOS_DEV_PROVISIONING_PROFILE=""


UnlockKeychain(){
    if [ -f $OSX_KEYCHAIN_AUTH_FILE ]
    then
        . $OSX_KEYCHAIN_AUTH_FILE
    else
        msg error "Failed to unlock keychain: $OSX_KEYCHAIN_AUTH_FILE not found"
    fi
    
    security unlock-keychain -p "$OSX_JENKINS_PASSWORD" $OSX_KEYCHAIN_FILE
    security set-keychain-settings -t 3600 -l $OSX_KEYCHAIN_FILE
}

XCodeBuild(){
    test -d $(dirname $ios_export_path) || mkdir -p $(dirname $ios_export_path)
    xcodebuild  -workspace $ios_project_path\
                -scheme $ios_scheme\
                -configuration $ios_configuration\
                -sdk $ios_sdk\
                CODE_SIGN_IDENTITY="$iOS_DEV_CSI"\
                PROVISIONING_PROFILE="$iOS_PROVISIONING_PROFILE"\
                -archivePath ${ios_archive_path}\
                clean archive

    xcodebuild  -exportArchive\
                -archivePath ${ios_archive_path}\
                PROVISIONING_PROFILE="${iOS_PROVISIONING_PROFILE}"\
                CODE_SIGN_IDENTITY="${iOS_DEV_CSI}"\
                -exportPath $ios_export_path 

    echo -e "Download URLs:"
    echo -e "\t1. $JKS_DL_URL/$iOS_IPA_SAVE_URL"
}
